// services/apiKeys.js
import { ipcMain } from 'electron'
import {
  getAllApiKeys,
  addApiKey,
  updateApiKey,
  deleteApiKey,
  toggleApiKeyStatus,
  getRandomEnabledApiKey,
  getApiKeyById
} from '../dao/apiKeys.js'

const MODEL_NAME = 'apiKey'

export function init() {
  // 获取所有API密钥
  ipcMain.handle(`${MODEL_NAME}/getAll`, () => {
    return getAllApiKeys()
  })

  // 添加新API密钥
  ipcMain.handle(`${MODEL_NAME}/add`, (event, name, key, description, enabled) => {
    return addApiKey(name, key, description, enabled)
  })

  // 更新API密钥
  ipcMain.handle(`${MODEL_NAME}/update`, (event, id, data) => {
    return updateApiKey(id, data)
  })

  // 删除API密钥
  ipcMain.handle(`${MODEL_NAME}/delete`, (event, id) => {
    return deleteApiKey(id)
  })

  // 启用/禁用API密钥
  ipcMain.handle(`${MODEL_NAME}/toggle`, (event, id, enabled) => {
    return toggleApiKeyStatus(id, enabled)
  })

  // 获取随机启用的API密钥
  ipcMain.handle(`${MODEL_NAME}/getRandomEnabled`, () => {
    return getRandomEnabledApiKey()
  })

  // 根据ID获取单个API密钥
  ipcMain.handle(`${MODEL_NAME}/getById`, (event, id) => {
    return getApiKeyById(id)
  })
}
